﻿
using System;
namespace ChangeListUtility
{
    class p4Wrapper
    {
        static string[] needLogin = { 
                                        "Your session has expired, please login again.\r\n", 
                                        "Perforce password (P4PASSWD) invalid or unset.\r\n"
                                    };

        bool bP4LoginExecuted = false; // TODO:

        public enum p4Depots
        {
            p4Depot_Acad,
            p4Depot_ACA,
            p4Depot_AIRMax,
        };

        static string[] depots = {
                                "shapsebp4p:1999", 
                                "shaaecp4p:1666", 
                                "psebp4:1444", 
                          };

        public enum p4Cmds
        {
            p4Cmd_Login, 
            p4Cmd_Describe, 
            p4Cmd_DescribeWorkspaceSpec, // TODO
            p4Cmd_PrintFile,
        };

        static string[] cmds = {
                                   "login", 
                                   "describe",
                                   "", 
                                   "print",
                               };

        // TODO
        public enum p4FileOperations
        {
            p4FileOp_edit, 
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static string getDepotOption(p4Depots depot)
        {
            return "-p " + depots[(int)depot];
        }


        private static string getP4Cmd(p4Cmds p4cmd)
        {
            return cmds[(int)p4cmd];
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strStdout"></param>
        /// <param name="strStderr"></param>
        /// <returns></returns>
        private bool isLogined(string strStdout, string strStderr)
        {
            int ind = Array.IndexOf(needLogin, strStderr);
            return !(strStdout == "" && (Array.IndexOf(needLogin, strStderr) != -1));
        }

        private bool isSucceeded(string strStdout, string strStderr)
        {
            // TODO: implementation
            return true;
        }

        private bool executeP4Login(p4Depots p4depot)
        {
            string cmd = "p4 " + getDepotOption(p4depot) + " " + getP4Cmd(p4Cmds.p4Cmd_Login);
            CmdExecuter.executeCmd(cmd);

            // TODO 
            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="depot"></param>
        /// <param name="output"></param>
        /// <returns></returns>
        public bool executeP4Cmd(p4Cmds p4cmd, p4Depots p4depot, string cmdOption, out string output)
        {
            string localOutput, localError;

            string cmd = "p4 " + getDepotOption(p4depot) + " " + getP4Cmd(p4cmd) + " " + cmdOption;
            
            CmdExecuter.executeCmd(cmd, out localOutput, out localError);
            if (!isLogined(localOutput, localError) && p4cmd != p4Cmds.p4Cmd_Login)
            {
                // login 
                if (executeP4Login(p4depot))
                {
                    //reexecute 
                    CmdExecuter.executeCmd(cmd, out localOutput, out localError);
                }
            }
            output = localOutput;
            return isSucceeded(localOutput, localError);
        }
    }
}
